package com.ui.mine.view;

import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.BaseRefreshFragment;
import com.bean.LoginBean;
import com.ddd.wandemo.R;
import com.ui.mine.presenter.RegisterFragmentContract;
import com.ui.mine.presenter.RegisterFragmentPresenter;
import com.util.ToastUtil;

public class RegisterFragment extends BaseRefreshFragment<RegisterFragmentPresenter> implements RegisterFragmentContract.View {
    private EditText etAccount, etPassword,etRePassword;
    private TextView tvLogin;
    private String account, pwd,repwd;
    private ImageView ivShowPwd,ivShowRePwd;
    private boolean showPwd = false;
    private boolean showRePwd = false;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_register;
    }

    @Override
    protected void initEventAndData() {
        delayLoad = false;
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                account = etAccount.getText().toString().trim();
                pwd = etPassword.getText().toString().trim();
                repwd = etRePassword.getText().toString().trim();
                if (TextUtils.isEmpty(account) & TextUtils.isEmpty(pwd)& TextUtils.isEmpty(repwd)) {
                    ToastUtil.showToast("请输入账号跟密码");
                } else {
                    mPresenter.doRegister(account,pwd,repwd);
                }
            }
        });

        //edittext输入监听
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() > 0) {
                    ivShowPwd.setVisibility(View.VISIBLE);
                } else {
                    ivShowPwd.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });


        ivShowPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPwd = !showPwd;
                if (showPwd) {
                    etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                    etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                ivShowPwd.setSelected(showPwd);
            }
        });

        ivShowRePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRePwd = !showRePwd;
                if (showRePwd) {
                    etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                    etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                ivShowPwd.setSelected(showRePwd);
            }
        });
    }

    @Override
    public void onRegister(LoginBean bean) {

    }

    @Override
    protected void initView() {
        etAccount = view.findViewById(R.id.et_login_account);
        etPassword = view.findViewById(R.id.et_login_pwd);
        etRePassword = view.findViewById(R.id.et_login_repwd);
        tvLogin = view.findViewById(R.id.tv_do_login);
        ivShowPwd = view.findViewById(R.id.iv_show_pwd);
        ivShowRePwd = view.findViewById(R.id.iv_show_repwd);
    }

    @Override
    protected void loadData() {

    }
    @Override
    protected RegisterFragmentPresenter initPresenter() {
        return new RegisterFragmentPresenter();
    }

}
